package com.example.sdkintegration.controller;

import com.example.sdkintegration.feign.DemoServer;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

/**
 * 监控&链路测试Controller
 */
@RestController
@RequestMapping("/api/${api-module:}/chain/test")
public class ChainTestController {
    @Autowired
    private OkHttpClient okHttpClient;

    @Autowired
    private HttpClient httpClient;

    @Autowired
    private DemoServer demoServer;

    @GetMapping
    private String get() {
        return "1";
    }
    private static int index = 0;
    @GetMapping("/feign")
    private String feign() {
        System.out.println(index++);
        return demoServer.get();
    }

    @GetMapping("/okhttp")
    private String okhttp() {
        String url = "http://127.0.0.1:8080/api/app/pivottest/test";
        Request request = new Request.Builder().url(url).build();
        try {
            Response response = okHttpClient.newCall(request).execute();
            return response.body().string();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "0";
    }

    @GetMapping("/httpclient")
    private String httpclient() {
        HttpGet httpGet = new HttpGet("http://127.0.0.1:8080/api/app/pivottest/test");
        HttpResponse response = null;
        try {
            response = httpClient.execute(httpGet);
            return EntityUtils.toString(response.getEntity());
        } catch (IOException e) {
            e.printStackTrace();

        }
        return "0";
    }

}
